package com.carey.blog.repository.impl;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import com.carey.blog.framework.latke.repository.RepositoryException;
import com.carey.blog.framework.latke.repository.gae.AbstractGAERepository;
import com.carey.blog.model.Link;
import com.carey.blog.repository.LinkRepository;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

/**
 * Link Google App Engine repository.
 * 
 */
public final class LinkGAERepository extends AbstractGAERepository implements
		LinkRepository {

	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(LinkGAERepository.class.getName());

	@Override
	public String getName() {
		return Link.LINK;
	}

	@Override
	public JSONObject getByAddress(final String address) {
		final Query query = new Query(getName());
		query.addFilter(Link.LINK_ADDRESS, Query.FilterOperator.EQUAL, address);
		final PreparedQuery preparedQuery = getDatastoreService()
				.prepare(query);
		final Entity entity = preparedQuery.asSingleEntity();
		if (null == entity) {
			return null;
		}

		final Map<String, Object> properties = entity.getProperties();

		return new JSONObject(properties);
	}

	@Override
	public int getMaxOrder() throws RepositoryException {
		final Query query = new Query(getName());
		query.addSort(Link.LINK_ORDER, Query.SortDirection.DESCENDING);
		final PreparedQuery preparedQuery = getDatastoreService()
				.prepare(query);
		final List<Entity> links = preparedQuery.asList(FetchOptions.Builder
				.withDefaults());
		if (links.isEmpty()) {
			return -1;
		}

		try {
			final Map<String, Object> properties = links.get(0).getProperties();
			return new JSONObject(properties).getInt(Link.LINK_ORDER);
		} catch (final JSONException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new RepositoryException(e);
		}
	}

	@Override
	public JSONObject getByOrder(final int order) {
		final Query query = new Query(getName());
		query.addFilter(Link.LINK_ORDER, Query.FilterOperator.EQUAL, order);
		final PreparedQuery preparedQuery = getDatastoreService()
				.prepare(query);
		final Entity entity = preparedQuery.asSingleEntity();
		if (null == entity) {
			return null;
		}

		final Map<String, Object> properties = entity.getProperties();

		return new JSONObject(properties);
	}

	/**
	 * Gets the {@link LinkGAERepository} singleton.
	 * 
	 * @return the singleton
	 */
	public static LinkGAERepository getInstance() {
		return SingletonHolder.SINGLETON;
	}

	/**
	 * Private default constructor.
	 */
	private LinkGAERepository() {
	}

	/**
	 * Singleton holder.
	 * 
	 */
	private static final class SingletonHolder {

		/**
		 * Singleton.
		 */
		private static final LinkGAERepository SINGLETON = new LinkGAERepository();

		/**
		 * Private default constructor.
		 */
		private SingletonHolder() {
		}
	}
}
